﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Twinkle.Dialogs.Hashtag
{
    public partial class BindingConfig : Form
    {
        public BindingConfig()
        {
            InitializeComponent();
            RefreshList();
        }

        private void RefreshList()
        {
            bindingList.Items.Clear();
            if (Kernel.Config.Behavior.TagAutoBindingData != null)
            {
                foreach (var item in Kernel.Config.Behavior.TagAutoBindingData)
                {
                    bindingList.Items.Add(new ListViewItem(new string[] { item.Keyword, item.Type.ToString(), item.Tags }));
                }
            }
        }

        private void addItem_Click(object sender, EventArgs e)
        {
            using (var be = new BindingEditor())
            {
                be.Binding = new Data.Config.BehaviorConfig.TagBinding();
                if (be.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    var item = be.Binding;
                    List<Data.Config.BehaviorConfig.TagBinding> tagb = new List<Data.Config.BehaviorConfig.TagBinding>();
                    if (Kernel.Config.Behavior.TagAutoBindingData != null)
                        tagb.AddRange(Kernel.Config.Behavior.TagAutoBindingData);
                    tagb.Add(item);
                    Kernel.Config.Behavior.TagAutoBindingData = tagb.ToArray();
                    RefreshList();
                }
            }
        }

        private void editItem_Click(object sender, EventArgs e)
        {
            if (bindingList.SelectedIndices.Count == 0)
                return;
            using (var be = new BindingEditor())
            {
                be.Binding = Kernel.Config.Behavior.TagAutoBindingData[bindingList.SelectedIndices[0]];
                if(be.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Kernel.Config.Behavior.TagAutoBindingData[bindingList.SelectedIndices[0]] = be.Binding;
                    RefreshList();
                }
            }
        }

        private void removeItem_Click(object sender, EventArgs e)
        {
            if (bindingList.SelectedIndices.Count == 0)
                return;
            List<Data.Config.BehaviorConfig.TagBinding> tagb = new List<Data.Config.BehaviorConfig.TagBinding>();
            tagb.AddRange(Kernel.Config.Behavior.TagAutoBindingData);
            tagb.RemoveAt(bindingList.SelectedIndices[0]);
            Kernel.Config.Behavior.TagAutoBindingData = tagb.ToArray();
            RefreshList();
        }

        private void CloseBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
